
#include <gtest/gtest.h>

#include "nane/lib/nane.h"
#include "nane/core/crt/filesystem.h"

TEST( NaneTest, FileSystemCheck )
{
    ASSERT_TRUE( nane::Initialize() );
    nane::WriteFilePtr writeFile = nane::FileSystem::OpenWriteFile("test.txt");
    ASSERT_TRUE( writeFile != nane::NullPtr );
    std::string outputData( "Hello, world!" );
    nane::uint32 bytesWritten = writeFile->Write( reinterpret_cast<const nane::byte*>( outputData.c_str() ), outputData.size() );
    EXPECT_EQ( 13U, bytesWritten );
    //nane::FileSystem::closeFile(file);
    writeFile = nane::NullPtr;
    
    nane::ReadFilePtr readFile = nane::FileSystem::OpenReadFile("test.txt");
    ASSERT_TRUE( readFile != nane::NullPtr );
    nane::uint32 fileSize = readFile->Size();
    EXPECT_EQ( bytesWritten, fileSize );
    std::vector<nane::byte> inputData;
    inputData.resize( fileSize + 1, 0 );
    EXPECT_EQ( fileSize, readFile->Read( &(inputData[0]), fileSize ) );
    EXPECT_STREQ( outputData.c_str(), reinterpret_cast<const char*>( &(inputData[0]) ) );
    //nane::FileSystem::closeFile(file);
    readFile = nane::NullPtr;

    EXPECT_TRUE(nane::FileSystem::CreateFolder("testfolder"));
    writeFile = nane::FileSystem::OpenWriteFile("testfolder/test.txt");
    ASSERT_TRUE( writeFile != nane::NullPtr );
    //nane::FileSystem::closeFile(file);
    writeFile = nane::NullPtr;
    EXPECT_FALSE( nane::FileSystem::DeleteFolder("testfolder") );
    EXPECT_TRUE(nane::FileSystem::DeleteFile("testfolder/test.txt"));
    EXPECT_TRUE(nane::FileSystem::DeleteFolder("testfolder"));

    nane::Shutdown();
}
